<?php
/*
Plugin Name: WP AutoSuggest
Plugin URI: http://www.ekhoury.com/wp-autosuggest/
Description: This plugin adds Auto suggest support to your WordPress searchbox!
Version: 0.1
Author: Elie El Khoury
Author URI: http://www.ekhoury.com
*/

require_once($_SERVER["DOCUMENT_ROOT"] . '/wp-config.php');
require_once($_SERVER["DOCUMENT_ROOT"] . '/wp-includes/pluggable.php');
require_once( 'autosuggest_functions.php' );
require_once($_SERVER["DOCUMENT_ROOT"] . '/wp-content/mu-plugins/bp-core.php');
require_once($_SERVER["DOCUMENT_ROOT"] . '/wp-content/mu-plugins/bp-groups.php');


global $wpdb, $bp;

bp_core_setup_globals();
groups_setup_globals();

$bpdev_as_action = '';
$bpdev_as_keys = '';

if(isset($_GET['bpdev_as_action'])) {

	$bpdev_as_action = $_GET['bpdev_as_action'];

}
if (isset($_GET['bpdev_as_keys'])) {

	$bpdev_as_keys = $_GET['bpdev_as_keys'];

}


if ($bpdev_as_action == 'query') {

	header('Content-Type: text/xml');
	echo '<results>';
	
	global $wpdb;
	
	$bpdev_as_keys = str_replace(' ','%',$bpdev_as_keys);

	$members = BP_Core_User::search_users( $bpdev_as_keys );
	$groups = BP_Groups_Group::search_groups( $bpdev_as_keys );

	foreach ( $members['users'] as $member ) {

		echo "<rs id=\"";
		$user = new BP_Core_User( $member->user_id );
		echo $user->user_url;
		echo "\" info=\"" . htmlentities( bp_last_activity( $member->user_id, false ) ) . "\" img=\"" . bp_core_get_avatar( $member->user_id, 1, 30, 30, true ) . "\">";
		echo $user->fullname;
		echo "</rs>";

	}

	foreach ($groups['groups'] as $group) {

		echo "<rs id=\"";
		$group_oj = new BP_Groups_Group( $group->group_id , true );
		echo bp_group_permalink( $group_oj, false );
		echo "\" info=\"" . htmlentities( $group_oj->description ) . "\" img=\"" . $group_oj->avatar_thumb . "\">";
		echo $group_oj->name;
		echo "</rs>";

	}

	echo '</results>';
	die();
}

?>